package com.link.base.base.projectmanage.service;

import com.link.base.base.projectmanage.dao.mybatis.mapper.PartRegisterMapper;
import com.link.base.base.projectmanage.model.PartRegister;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 零件登记
 *
 * @author 刘启鑫
 * @date 2020-09-11 17:13:24
 */
@Service
public class PartRegisterServiceImpl extends BasicServiceImpl<PartRegister> implements PartRegisterService {

    private static final Logger logger = LoggerFactory.getLogger(PartRegisterServiceImpl.class);

    @Resource
    private PartRegisterMapper partRegisterMapper;

    @Resource
    private TaskInfoService taskInfoService;

    @Override
    public BasicMapper<PartRegister> getBasicMapper() {
        return partRegisterMapper;
    }

    @Override
    public void beforDelete(PartRegister entity) throws Exception {
        super.beforDelete(entity);
        //关联删除零件对应拜访计划  弃
//        entity = partRegisterMapper.querySonByPartRegId(entity);
//        if (!Objects.isNull(entity)) {
//            List<TaskInfo> taskInfoList = entity.getTaskInfos();
//            if (!CollectionUtils.isEmpty(taskInfoList)) {
//                for (TaskInfo taskInfo : taskInfoList) {
//                    taskInfoService.deleteById(taskInfo);
//                }
//            }
//        }
    }

    public void beforUpsert(PartRegister entity) throws Exception {
        super.beforUpsert(entity);
        PartRegister queryOne = new PartRegister();
        queryOne.setCarRegisterId(entity.getCarRegisterId());
        queryOne.setEstPartType(entity.getEstPartType());
        List<PartRegister> partRegisterList = queryByExamplePage(entity);
        if (!CollectionUtils.isEmpty(partRegisterList) && partRegisterList.size() > 1) {
            throw new ServiceException("IYUPARTREG-001");
        }
        if (partRegisterList.size() == 1) {
            if (entity.getId() != null && !entity.getId().equals(partRegisterList.get(0).getId())) {
                throw new ServiceException("IYUPARTREG-001");
            }
        }
    }
}
